//
//  DebarkViewController.m
//  UI - 豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "DebarkViewController.h"
#import "RegisterViewController.h"
#import "FourthViewController.h"
#import "DateBaseSingleton.h"
#import "userDebark.h"

@interface DebarkViewController ()
@property(nonatomic, retain)UILabel *lable;
@property(nonatomic, retain)UITextField *textField;
@property(nonatomic, retain)UITextField *textField1;
@end

@implementation DebarkViewController
- (void)dealloc
{
    [_textField release];
    [_lable release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.num = 0;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(30, 40, 100, 40)];
    self.lable.text =@"用户名：";
    self.lable.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:self.lable];
    [_lable release];
    
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(120, 45, 200, 40)];
    self.textField.clearButtonMode = YES;
    self.textField.placeholder = @"请输入账号";
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:self.textField];
    [self.textField release];
    
    
    
    
    self.lable = [[UILabel alloc] initWithFrame:CGRectMake(30, 110, 100, 40)];
    self.lable.text =@"密码：";
    self.lable.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:self.lable];
    [_lable release];
    
    self.textField1 = [[UITextField alloc] initWithFrame:CGRectMake(120, 110, 200, 40)];
    self.textField1.clearButtonMode = YES;
    self.textField1.placeholder = @"请输入密码";
    self.textField1.borderStyle = UITextBorderStyleRoundedRect;

    self.textField1.secureTextEntry = YES;
    [self.view addSubview:self.textField1];
    [self.textField1 release];
    
    
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(80, 200, 40, 20);
    [btn1 setTitle:@"登录" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(250, 200, 40, 20);
    [btn2 setTitle:@"注册" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(buttonAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    
    
}

-(void)buttonAction: (UIButton *)button{
    
    NSMutableArray *array = [NSMutableArray array];
    array = [[DateBaseSingleton shareDataBase] selectDebarkUser];
    for (int i = 0 ; i < array.count; i++) {
        userDebark *user = array[i];
        if ((self.textField.text == user.name) && (self.textField1.text == user.passWord)) {
            self.num = 1;
            [self.fourViewController precentFouthViewController:self.num];
            
            [[NSUserDefaults standardUserDefaults] setObject:self.textField.text forKey:@"user"];
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"很遗憾" message:@"登陆成功" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }];
            [alertC addAction:action];
            
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
        }
        }
    
    if (self.textField.text.length == 0 || self.textField1.text.length == 0) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号或密码不能为空" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
            
            
        }];
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
        
    }
    else{
    
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入正确的账号或密码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            
            
            
        }];
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
    }
    
    
    
}
-(void)buttonAction1: (UIButton *)button{

    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
